package JPetStore.web.servlet;

import JPetStore.domain.Account;
import JPetStore.domain.Cart;
import JPetStore.domain.Product;
import JPetStore.persistence.CartDao;
import JPetStore.persistence.impl.CartDaoImpl;
import JPetStore.service.AccountService;
import JPetStore.service.CatalogService;
import JPetStore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SignOnServlet extends HttpServlet {
    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";

    private String username;
    private String password;
    private String value1;
    private String value2;
    private String msg;
    Boolean isSame = false;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");
        this.value1 = req.getParameter("vCode");
        this.value2 = (String)session.getAttribute("checkcode");

        // 校验用户输入的正确性
        if(!validate()){
            req.setAttribute("signOnMsg", this.msg);
            req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
        } else {
            AccountService accountService = new AccountService();
            Account loginAccount = accountService.getAccount(username, password);
            if(loginAccount == null){
                this.msg = "用户名或密码错误";
                req.setAttribute("signOnMsg", this.msg);
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
            } else if (this.value1.isEmpty()) {
                this.msg = "验证码为空";
                req.setAttribute("signOnMsg", this.msg);
                req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
            } else {
                if (isSame) {
                    loginAccount.setUserid(username);
                    loginAccount.setPassword(null);
                    session.setAttribute("loginAccount", loginAccount);
                    session.setAttribute("password",this.password);
                    session.setAttribute("username",this.username);
                    if(loginAccount.isListOption()){
                        CatalogService catalogService = new CatalogService();
                        List<Product> myList = catalogService.getProductListByCategory(loginAccount.getFavouriteCategoryId());
                        session.setAttribute("myList", myList);
                    }
                    if (loginAccount != null) {
                        String username = loginAccount.getUsername();
                        String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                                + req.getContextPath() + req.getServletPath() + "?" + (req.getQueryString());
                        LogService logService = new LogService();
                        String logInfo = logService.logInfo(new Object[]{" "}) + strBackUrl + "登陆了";
                        logService.insertLogInfo(username, logInfo);
                    }

                    Cart cart = new Cart();
                    CartDao cartDao = new CartDaoImpl();
                    cartDao.getCart(loginAccount,cart.getCartItemList());//实时查看数据
                    session.setAttribute("cart", cart);
                    resp.sendRedirect("mainForm");
                } else {
                    this.msg = "验证码错误";
                    req.setAttribute("signOnMsg", this.msg);
                    req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
                }
            }
        }
    }

    private boolean validate() {
        if(this.username == null || this.username.equals("") || this.password == null || this.password.equals("")) {
            this.msg = "用户名不能为空";
            return false;
        }
        if(this.password == null || this.password.equals("") || this.password == null || this.password.equals("")) {
            this.msg = "密码不能为空";
            return false;
        }
        if(password.length() > 20) {
            this.msg = "密码长度必须在20位以内";
            return false;
        }
        if(value2.equalsIgnoreCase(value1)){
            isSame = true;
        }
        return true;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}


